﻿#region Header
// ------------------------ Licence / Copyright ------------------------
// 
// ScrumTable for Scrum Meeting Support
// Copyright © HSR - Hochschule für Technik Rapperswil
// All Rights Reserved
// 
// Author:
//  Michael Gfeller Silvan Gehrig Patrick Boos
// 
// ---------------------------------------------------------------------
#endregion

#region Usings

using System;
using System.Reflection;
using ScrumTable.Common.Properties;

#endregion

namespace ScrumTable.Common.Config.Xml
{
    /// <summary>
    /// Represents the root driver configuration node.
    /// </summary>
    internal class XmlConfigProperty : IXmlConfigProperty 
    {
        #region Declarations
        //--------------------------------------------------------------------
        // Declarations
        //--------------------------------------------------------------------

        private readonly PropertyInfo _property;
        private readonly IXmlConfigSection _target;

        #endregion
        
        #region Properties
        //--------------------------------------------------------------------
        // Properties
        //--------------------------------------------------------------------

        /// <summary>
        /// Returns the configuration property name.
        /// </summary>
        public string PropertyName { get { return _property.Name; } }

        /// <summary>
        /// Returns the type of the value for the property to configure.
        /// </summary>
        public Type Type { get { return _property.GetGetMethod().ReturnType; } }

        /// <summary>
        /// Returns the value of the property to configure.
        /// </summary>
        public object Value
        {
            get { return _property.GetGetMethod().Invoke(_target, null); }
            set
            {
                if (IsReadOnly)
                {
                    throw new InvalidOperationException(string.Format(Resources.ExcXmlCnfPropReadOnly, value));
                }
                _property.GetSetMethod().Invoke(_target, new [] { value } );
            }
        }

        /// <summary>
        /// Returns true if the property is read only and should not be
        /// set over the config property API.
        /// </summary>
        public bool IsReadOnly { get; private set; }

        #endregion
        
        #region Constructors / Destructor

        //--------------------------------------------------------------------
        // Constructors / Destructor
        //--------------------------------------------------------------------

        /// <summary>
        /// Initializes a new instance of the <see cref="XmlConfigProperty"/> class.
        /// </summary>
        internal XmlConfigProperty(IXmlConfigSection targetObject, PropertyInfo property, bool isReadOnly)
        {
            PreCondition.AssertNotNull(targetObject, "targetObject");
            PreCondition.AssertNotNull(property, "property");
            PreCondition.AssertTrue(property.GetGetMethod() != null && property.GetSetMethod() != null, Resources.ExcXmlCnfPropInvaildPropertyInfo);

            IsReadOnly = isReadOnly;

            _target = targetObject;
            _property = property;
        }

        #endregion

        #region Methods
        //--------------------------------------------------------------------
        // Methods
        //--------------------------------------------------------------------

        #endregion

        #region Events
        //--------------------------------------------------------------------
        // Events
        //--------------------------------------------------------------------

        #endregion
    }
}
